package com.dearbinge.data.service.impl.im;

import com.baomidou.mybatisplus.plugins.Page;
import com.dearbinge.data.api.im.UserApplyService;
import com.dearbinge.data.api.im.UserContactService;
import com.dearbinge.data.api.user.UserService;
import com.dearbinge.data.dao.UserSolicitudeMapper;
import com.dearbinge.data.dao.im.ImUserApplyMapper;
import com.dearbinge.data.dao.im.ImUserContactMapper;
import com.dearbinge.data.pojo.im.ImUserApply;
import com.dearbinge.data.pojo.im.ImUserContact;
import com.dearbinge.data.pojo.im.vo.ImUserApplyVO;
import com.dearbinge.data.pojo.user.User;
import com.dearbinge.data.pojo.userSolicitude.UserSolicitude;
import com.dearbinge.utils.Base64Util;
import com.dearbinge.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 好友申请业务实现
 *
 * @author wes
 * @date 2019/2/27
 */
@Service(value = "userApplyService")
public class UserApplyServiceImpl implements UserApplyService {

    @Autowired
    private ImUserApplyMapper imUserApplyMapper;
    @Autowired
    private UserContactService userContactService;
    @Autowired
    private ImUserContactMapper imUserContactMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private UserSolicitudeMapper userSolicitudeMapper;
    /**
     * 删除
     *
     * @param id id
     * @return 结果
     */
    @Override
    public Integer delete(Integer id) {
        return imUserApplyMapper.deleteByPrimaryKey(id);
    }

    /**
     * 更新
     *
     * @param userApply 好友申请
     * @return 结果
     */
    @Override
    public Integer update(ImUserApply userApply) {
        //同意 插入好友表 同时强制互相关注
        if (userApply.getAuditStatus() == 1 ) {
            ImUserApply imUserApply = imUserApplyMapper.getByPrimaryKey(userApply.getId());
            Integer userId = imUserApply.getUserId();
            Integer friendId = imUserApply.getFriendId();
            if (userId.equals(friendId)) {
                return 5002;
            }
            insertContact(userId, friendId);
            insertContact(friendId,userId);
            //强制互相关注
            insertSolicitude(userId, friendId);
            insertSolicitude( friendId,userId);
        }
        return imUserApplyMapper.update(userApply);
    }

    private void insertSolicitude(Integer userId, Integer friendId) {
        if (StringUtils.isEmpty(userSolicitudeMapper.getIsSolicitude(userId.toString(), friendId.toString()))) {
            UserSolicitude userSolicitude = new UserSolicitude();
            userSolicitude.setUserId(userId.toString());
            userSolicitude.setSolicitudeId(friendId.toString());
            userSolicitude.setCreateBy(userId.toString());
            userSolicitude.setModifyBy(userId.toString());
            userSolicitudeMapper.insert(userSolicitude);
        }
    }

    private void insertContact(Integer userId, Integer friendId) {
        ImUserContact imUserContact = new ImUserContact();
        imUserContact.setUserId(userId);
        imUserContact.setFriendId(friendId);
        userContactService.insert(imUserContact);
    }

    /**
     * 插入
     *
     * @param userApply 好友申请
     * @return 结果
     */
    @Override
    public Integer insert(ImUserApply userApply) {
        //如果存在 则修改返回
        ImUserApplyVO userApplyVO = imUserApplyMapper.getByUserIdAndFriendId(userApply.getUserId(), userApply.getFriendId());
        if (userApplyVO != null ) {
            Integer friendId = userApply.getFriendId();
            Integer userId = userApply.getUserId();

            if (userId.equals(friendId)) {
                return 5002;
            }


            if (userApply.getValidateMessage() != null) {
                userApply.setId(userApplyVO.getId());
                return imUserApplyMapper.update(userApply);
            } else {
                return 1;
            }
        }
        return imUserApplyMapper.insert(userApply);
    }

    /**
     * 根据id查询
     *
     * @param id id
     * @return 结果对象
     */
    @Override
    public ImUserApply getById(Integer id) {
        return imUserApplyMapper.getByPrimaryKey(id);
    }

    /**
     * 根据被申请用户ID查询好友申请列表
     *
     * @param friendId friendId
     * @return 申请列表
     */
    @Override
    public Page listByFriendId(Integer friendId,Integer current, Integer size) {
        current = PageUtils.getDefaultValues(current, 1);
        size = PageUtils.getDefaultValues(size, 10);
        Page<ImUserApplyVO> page = new Page<ImUserApplyVO>(current, size);
        List<ImUserApplyVO> list = imUserApplyMapper.listByFriendId(friendId);
        //遍历插入申请用户别名 名字  头像
        for (ImUserApplyVO imUserApplyVO : list) {
            Integer userId = imUserApplyVO.getUserId();
            ImUserContact contact = imUserContactMapper.getContact(friendId,userId);
            if (contact != null) {
                imUserApplyVO.setAlias(contact.getAlias());
            }
            User user = userService.getUserById(userId);
            imUserApplyVO.setHeadImg( user.getHeadImg());
            imUserApplyVO.setFriendName( Base64Util.decodeData(user.getName()));
        }
        Integer count = imUserApplyMapper.countByFriendId(friendId);
        page.setTotal(count);
        page.setRecords(list);
        return page;
    }


}
